package com.cheyun.netsalev3.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.cheyun.netsalev3.R
import com.cheyun.netsalev3.bean.NameIdParam
import com.cheyun.netsalev3.databinding.RecyclerItemBinding
import com.cheyun.netsalev3.viewmodel.HomeTaskFragment2ViewModel

/**
 * @author Michael
 * @date  2018/11/15 - 16:58.
 */
class NameTopAdapter(var viewModel: HomeTaskFragment2ViewModel) : RecyclerView.Adapter<NameTopAdapter.ViewHolder>() {
    lateinit var context: Context
    var lists = ArrayList<NameIdParam>()
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        context = parent.context
        return ViewHolder(RecyclerItemBinding.inflate(LayoutInflater.from(context), parent, false))
    }

    override fun getItemCount(): Int = Math.ceil(lists.size * 1.0 / 8).toInt()

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.apply {
            var layoutManager = object : GridLayoutManager(context, 4, GridLayoutManager.VERTICAL, false) {
                override fun canScrollVertically(): Boolean {
                    return false
                }
            }
            binding.recyclerView.layoutManager = layoutManager
            var adpetr = DefaultAdapter<NameIdParam>(R.layout.item_home_task1, viewModel)
            binding.recyclerView.adapter = adpetr
            var lastint = Math.ceil(lists.size * 1.0 / 8).toInt()
            var temlist = if (position == lastint - 1) {
                lists.subList(8 * position, lists.size)
            } else {
                lists.subList(8 * position, 8 * (position + 1))
            }

            adpetr.submitList(temlist)


        }
    }


    fun setData(lists: ArrayList<NameIdParam>) {
        this.lists = lists
        notifyDataSetChanged()
    }


    class ViewHolder(var binding: RecyclerItemBinding) : RecyclerView.ViewHolder(binding.root) {

    }
}